##########################################################################################################################################
###### This is the file containing the main merger regression results
###############################
# It creates the final results for Tables 5 and 12
# Please note that in some instances, the paper contains some formatting modifications
# such as a "log()" that is not automatically added here or that we present the negative
# of tsim, while most output here is generated for the positive

# This is just the final step. There are other code files that are used for data preparation
# This processed data are saved and loaded in this code

#######
# For Table 5, choose which part of the results you want:
specification="purpose-based"
#specification="tnic3"
#specification="sic"
#specification="not-anti"
#specification="placebo"
# the code will produce the corresponding part of the table. For the final tables,
# this could has to be run multiple times, once for each specification.

# set to your working directory:
setwd("C:\\Users\\Admin\\OneDrive\\Desktop\\Market Power and Systematic Risk - Data and Code")
getwd()

###################################
load("Mergers_with_SIC_NEW.RData")

Mergers_New$YearMonth = as.numeric(paste0(format(Mergers_New$Date.Effective.or.Unconditional,"%Y"),format(Mergers_New$Date.Effective.or.Unconditional,"%m")))
Mergers_New$YearMonth_ann = as.numeric(paste0(format(Mergers_New$Date.Announced,"%Y"),format(Mergers_New$Date.Announced,"%m")))
Mergers_New = Mergers_New[Mergers_New$Date.Announced>="1989-01-01",]

# Look at horizontal mergers (use the additional conditions described in the Li, 2022 paper)
Mergers_hor<-Mergers_New
Mergers_hor<-Mergers_hor[substr(Mergers_hor$Ac_ind,1,4)==substr(Mergers_hor$TAR_ind,1,4) & !is.na(Mergers_hor$Ac_ind),]
Mergers_hor=Mergers_hor[!is.na(Mergers_hor$Deal.PermID),]
# if SIC missing, use NAICS
Mergers_hor2<-Mergers_New
Mergers_hor2<-Mergers_hor2[((substr(Mergers_hor2$Ac_ind,1,4)==substr(Mergers_hor2$TAR_ind,1,4))|(substr(Mergers_hor2$Ac_ind2,1,3)==substr(Mergers_hor2$TAR_ind2,1,3)) ),]
Mergers_hor2=Mergers_hor2[!is.na(Mergers_hor2$Deal.PermID),]
Mergers_hor2<-Mergers_hor2[(Mergers_hor2$Ac_ind==Mergers_hor2$TAR_ind|is.na(Mergers_hor2$Ac_ind)|is.na(Mergers_hor2$TAR_ind)),]
Mergers_hor=Mergers_hor2
# Alternatively use the TNIC 300 industries
Mergers_hor3<-Mergers_New
Mergers_hor3<-Mergers_hor3[!is.na(Mergers_hor3$similarity),]
Mergers_hor3=Mergers_hor3[!is.na(Mergers_hor3$Deal.PermID),]
if(specification=="tnic3"){Mergers_hor=Mergers_hor3}

# mergers that are not anticompetitive on any of the definitions
Mergers_not_anticom=Mergers_New[is.na(Mergers_New$similarity),]
Mergers_not_anticom=Mergers_not_anticom[((substr(Mergers_not_anticom$Ac_ind,1,4)!=substr(Mergers_not_anticom$TAR_ind,1,4)|is.na(Mergers_not_anticom$TAR_ind)|is.na(Mergers_not_anticom$Ac_ind))&(substr(Mergers_not_anticom$Ac_ind2,1,3)!=substr(Mergers_not_anticom$TAR_ind2,1,3)|is.na(Mergers_not_anticom$TAR_ind2)|is.na(Mergers_not_anticom$Ac_ind2)) ),]
Mergers_not_anticom=Mergers_not_anticom[!is.na(Mergers_not_anticom$Deal.PermID),]

# still a reasonable number of mergers left
Mergers_hor<-Mergers_hor[Mergers_hor$M.A.Type!="Repurchases Deal",]
Mergers_hor<-Mergers_hor[Mergers_hor$M.A.Type!="Self Tender Or Recapitalization Deal",]

Mergers_hor$Percentage.of.Shares.Held.at.Announcement[is.na(Mergers_hor$Percentage.of.Shares.Held.at.Announcement)]=0
Mergers_hor$Held_after=Mergers_hor$Percentage.of.Shares.Held.at.Announcement+Mergers_hor$Percentage.of.Shares.Acquired.in.Transaction

Mergers_hor_final = Mergers_hor[Mergers_hor$Percentage.of.Shares.Held.at.Announcement<10 | is.na(Mergers_hor$Percentage.of.Shares.Held.at.Announcement),]
Mergers_hor_final = Mergers_hor_final[Mergers_hor_final$Percentage.of.Shares.Acquired.in.Transaction > 50, ]
Mergers_hor_placebo<-Mergers_hor[Mergers_hor$Deal.Status=="Withdrawn",]

Mergers_New = Mergers_New[Mergers_New$Deal.Status == "Completed",]
Mergers_not_anticom = Mergers_not_anticom[Mergers_not_anticom$Deal.Status == "Completed",]


Mergers_not_anticom = Mergers_not_anticom[Mergers_not_anticom$Percentage.of.Shares.Held.at.Announcement<10 | is.na(Mergers_not_anticom$Percentage.of.Shares.Held.at.Announcement),]
Mergers_New = Mergers_New[Mergers_New$Percentage.of.Shares.Held.at.Announcement<10 | is.na(Mergers_New$Percentage.of.Shares.Held.at.Announcement),]

#only obtain mergers where at least 50 % of the Stock was Aquired:
Mergers_New = Mergers_New[Mergers_New$Percentage.of.Shares.Acquired.in.Transaction > 50 | is.na(Mergers_New$Percentage.of.Shares.Acquired.in.Transaction), ]
Mergers_not_anticom = Mergers_not_anticom[Mergers_not_anticom$Percentage.of.Shares.Acquired.in.Transaction > 50 | is.na(Mergers_not_anticom$Percentage.of.Shares.Acquired.in.Transaction), ]


#Specify the M.A.Type
Mergers_New = Mergers_New[Mergers_New$M.A.Type == "Disclosed Dollar Value Deal" | 
                            Mergers_New$M.A.Type == "Undisclosed Dollar Value Deal",]
Mergers_not_anticom = Mergers_not_anticom[Mergers_not_anticom$M.A.Type == "Disclosed Dollar Value Deal" | 
                                            Mergers_not_anticom$M.A.Type == "Undisclosed Dollar Value Deal",]

sum(is.na(Mergers_New$Acquiror.6.digit.CUSIP))

Mergers_New = Mergers_New[Mergers_New$Acquiror.6.digit.CUSIP != Mergers_New$Target.6.digit.CUSIP & !is.na(Mergers_New$Acquiror.6.digit.CUSIP),]
Mergers_not_anticom = Mergers_not_anticom[Mergers_not_anticom$Acquiror.6.digit.CUSIP != Mergers_not_anticom$Target.6.digit.CUSIP & !is.na(Mergers_not_anticom$Acquiror.6.digit.CUSIP),]
Mergers_hor_placebo = Mergers_hor_placebo[Mergers_hor_placebo$Acquiror.6.digit.CUSIP != Mergers_hor_placebo$Target.6.digit.CUSIP & !is.na(Mergers_hor_placebo$Acquiror.6.digit.CUSIP),]
Mergers_hor_final = Mergers_hor_final[Mergers_hor_final$Acquiror.6.digit.CUSIP != Mergers_hor_final$Target.6.digit.CUSIP & !is.na(Mergers_hor_final$Acquiror.6.digit.CUSIP),]

################################ Specifications #########################################################

Mergers_New = Mergers_New[apply(sapply(c("Acquire competitors technology/strategic assets",
                                         "Strengthen existing operations/expand presence in primary market",
                                         "Strengthen operations", "Create synergies", "Concentrate on core businesses/assets"), 
                                       grepl, Mergers_New$Purpose),1,any),]
Mergers_not_anticom = Mergers_not_anticom[!apply(sapply(c("Acquire competitors technology/strategic assets",
                                                          "Strengthen existing operations/expand presence in primary market",
                                                          "Strengthen operations", "Create synergies", "Concentrate on core businesses/assets"), 
                                                        grepl, Mergers_not_anticom$Purpose),1,any),]

##########################################################

load("Control_Panel.RData")

Panel_New<-Panel_New[order(Panel_New$YearMonth),]
Panel_New$Weight_Beta_M_lag=Panel_New$Weight_Beta_M
require(plyr)
Panel_New <- ddply( 
  Panel_New, .(PERMNO), transform,
  # This assumes that the data is sorted
  Weight_Beta_M_lag = c( NA, Weight_Beta_M[-length(Weight_Beta_M)] ) 
)

Panel_New$six_digit_cusip = substr(Panel_New$cusip,1,6)

Mergers_New = Mergers_New[Mergers_New$Acquiror.6.digit.CUSIP%in%Panel_New$six_digit_cusip,]
Mergers_not_anticom = Mergers_not_anticom[Mergers_not_anticom$Acquiror.6.digit.CUSIP%in%Panel_New$six_digit_cusip,]
Mergers_hor_placebo = Mergers_hor_placebo[Mergers_hor_placebo$Acquiror.6.digit.CUSIP%in%Panel_New$six_digit_cusip,]
Mergers_hor_final = Mergers_hor_final[Mergers_hor_final$Acquiror.6.digit.CUSIP%in%Panel_New$six_digit_cusip,]
Mergers_New$Year=floor(Mergers_New$YearMonth/100)
Mergers_New$Year=Mergers_New$Year+1
Mergers_New$Year2=Mergers_New$Year+1

Mergers_hor_final$Year=floor(Mergers_hor_final$YearMonth/100)
Mergers_hor_final$Year=Mergers_hor_final$Year+1
Mergers_hor_final$Year2=Mergers_hor_final$Year+1

############################################
#########
# pick the merger data based on which type shall be selected:
if(specification=="purpose-based"){
  Panel_New_all = merge(Panel_New,Mergers_New,by.x=c("YearMonth","six_digit_cusip"),by.y=c("YearMonth","Acquiror.6.digit.CUSIP"),all.x=T)
}
if(specification=="tnic3" || specification=="sic"){
  Panel_New_all = merge(Panel_New,Mergers_hor_final,by.x=c("YearMonth","six_digit_cusip"),by.y=c("YearMonth","Acquiror.6.digit.CUSIP"),all.x=T)
}
if(specification=="not-anti"){
  Panel_New_all = merge(Panel_New,Mergers_not_anticom,by.x=c("YearMonth","six_digit_cusip"),by.y=c("YearMonth","Acquiror.6.digit.CUSIP"),all.x=T)
}
if(specification=="placebo"){
  Panel_New_all = merge(Panel_New,Mergers_hor_placebo,by.x=c("YearMonth","six_digit_cusip"),by.y=c("YearMonth_ann","Acquiror.6.digit.CUSIP"),all.x=T)
}

#######################################################

Panel_New_all$COMNAM = NULL
Panel_New_all$conm = NULL
Panel_New_all$tic = NULL
Panel_New_all$TICKER = NULL

#Construct the Dummy for each Company/Year
################################################
Panel_New_all$Merger_Dummy = ifelse(is.na(Panel_New_all$Date.Announced),0,1)
###########################################################

sum(Panel_New_all$Merger_Dummy)
library(data.table)
setDT(Panel_New_all)
Panel_New_all = Panel_New_all[order(Panel_New_all$Time, decreasing = F),]


ExtAeND = function(x,days){
  #At this Point in Time a merger happened
  a = which(x==1)
  if(length(a)>1){
    b = pmin(which(x==1) + days,length(x))
    c = mapply(seq,a,b,SIMPLIFY = F)
    x[unique(unlist(c))] = 1
  }
  
  return(x)
}
ExtAeNDb = function(x,days){
  #At this Point in Time a merger happened
  a = which(x!=0)
  if(length(a)>1){
    b = pmin(which(x!=0) + days,length(x))
    for(i in 1:length(a)){
      x[a[i]:b[i]]<-x[a[i]]
    }
  }
  return(x)
}

qcut <- function(x, n) {
  cut(x, quantile(x, seq(0, 1, length = n + 1),'na.rm' = TRUE), labels = seq_len(n),
      include.lowest = TRUE)
}

Panel_New_all$Merger_Dummy3<-Panel_New_all$Merger_Dummy
Panel_New_all$Merger_Dummy6<-Panel_New_all$Merger_Dummy
Panel_New_all$Merger_Dummy12<-Panel_New_all$Merger_Dummy
Panel_New_all$Merger_Dummy24<-Panel_New_all$Merger_Dummy
Panel_New_all$Merger_Dummy36<-Panel_New_all$Merger_Dummy
Panel_New_all$Merger_Dummy60<-Panel_New_all$Merger_Dummy


Panel_New_all[,Merger_Dummy3:=ExtAeND(Merger_Dummy3,3),by=six_digit_cusip]
Panel_New_all[,Merger_Dummy6:=ExtAeND(Merger_Dummy6,6),by=six_digit_cusip]
Panel_New_all[,Merger_Dummy12:=ExtAeND(Merger_Dummy12,12),by=six_digit_cusip]
Panel_New_all[,Merger_Dummy24:=ExtAeND(Merger_Dummy24,24),by=six_digit_cusip]
Panel_New_all[,Merger_Dummy36:=ExtAeND(Merger_Dummy36,36),by=six_digit_cusip]
Panel_New_all[,Merger_Dummy60:=ExtAeND(Merger_Dummy60,60),by=six_digit_cusip]


#Conduct the Panel Regressions:
setDF(Panel_New_all)
Panel_New_all$year=Panel_New_all$year.x

Temp = Panel_New_all[,c("Time","YearMonth","year","NAICS","PERMNO","Weight_Beta_M","HHI","prodmktfluid","tnic3hhi","tnic3tsimm","Age","Momentum","Illiquidity","MktCap","Mkt_Book","Default_Spread",
                        "Financial_Leverage", "Operating_Leverage","Firm_size","InvestmentRates","ROE","Logat","Dividend_Dummy","q","I_Vol","Weight_Beta_CF","Weight_Beta_DR",
                        "Weight_Beta_Down","Weight_Beta_Up","Merger_Dummy3","Merger_Dummy6","Merger_Dummy12","Merger_Dummy24","Merger_Dummy36","Merger_Dummy60")]#

library(lfe)
for(i in 1:dim(Temp)[2]){
  Temp[,i][Temp[,i] == Inf] = NA
  Temp[,i][Temp[,i] == -Inf] = NA
}
Temp = na.omit(Temp)

source("Table_Functions.R")

if(specification=="purpose-based"){
  A3 = felm(Weight_Beta_M ~ Merger_Dummy3  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
              scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
  A6 = felm(Weight_Beta_M ~ Merger_Dummy6  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
              scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
  A12 = felm(Weight_Beta_M ~ Merger_Dummy12  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
               scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
}
A24 = felm(Weight_Beta_M ~ Merger_Dummy24  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
             scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
A36 = felm(Weight_Beta_M ~ Merger_Dummy36  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
             scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
if(specification=="purpose-based"){
  A60 = felm(Weight_Beta_M ~ Merger_Dummy60  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
               scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
}

Names = c("$M^D$","$log(Age)$","$log(AT)$","$Default~spread$","$Dividend$","$Financial~leverage$","$log(Firm~size)$","$Illiquidity$","$Investment~rate$","$iVol$",
          "$log(Mkt/Book)$","$Momentum$","$Operating~leverage$","$q$","$ROE$")

A<-A24
if(specification=="purpose-based"){
  Result = data.frame(Names=rep(NA,2*length(summary(A)$coef[,1])+2),'Mon24'=rep(NA,2*length(summary(A)$coef[,1])+2),'Mon36' = rep(NA,2*length(summary(A)$coef[,1])+2),
                      'Mon60'=rep(NA,2*length(summary(A)$coef[,1])+2),'Mon12' = rep(NA,2*length(summary(A)$coef[,1])+2), 'Mon6' = rep(NA,2*length(summary(A)$coef[,1])+2), 'Mon3' = rep(NA,2*length(summary(A)$coef[,1])+2))
}else {
  Result = data.frame(Names=rep(NA,2*length(summary(A)$coef[,1])+2),'Mon24'=rep(NA,2*length(summary(A)$coef[,1])+2),'Mon36' = rep(NA,2*length(summary(A)$coef[,1])+2))
}
Result$Names[seq(from=1,to=(nrow(Result)-2),by=2)] = Names
Result$Names[nrow(Result)-1] = "$R^2$"

A<-A24
Result$Mon24[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A)$coefficients[,1],digits=3),digits=3)),star_function(summary(A)$coefficients[,4]))
Result$Mon24[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A)$coefficients[,3],digits=3),digits=3),")")
Result$Mon24[nrow(Result)-1] = Lag_zero(round(summary(A)$r.sq * 100, digits = 2),digits=2)
Result$Mon24[nrow(Result)] = nrow(Mergers_New)

A<-A36
Result$Mon36[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A)$coefficients[,1],digits=3),digits=3)),star_function(summary(A)$coefficients[,4]))
Result$Mon36[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A)$coefficients[,3],digits=3),digits=3),")")
Result$Mon36[nrow(Result)-1] = Lag_zero(round(summary(A)$r.sq * 100, digits = 2),digits=2)
Result$Mon36[nrow(Result)] = nrow(Mergers_New)

if(specification=="purpose-based"){
  A<-A60
  Result$Mon60[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A)$coefficients[,1],digits=3),digits=3)),star_function(summary(A)$coefficients[,4]))
  Result$Mon60[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A)$coefficients[,3],digits=3),digits=3),")")
  Result$Mon60[nrow(Result)-1] = Lag_zero(round(summary(A)$r.sq * 100, digits = 2),digits=2)
  Result$Mon60[nrow(Result)] = nrow(Mergers_New)
  
  A<-A12
  Result$Mon12[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A)$coefficients[,1],digits=3),digits=3)),star_function(summary(A)$coefficients[,4]))
  Result$Mon12[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A)$coefficients[,3],digits=3),digits=3),")")
  Result$Mon12[nrow(Result)-1] = Lag_zero(round(summary(A)$r.sq * 100, digits = 2),digits=2)
  Result$Mon12[nrow(Result)] = nrow(Mergers_New)
  
  A<-A6
  Result$Mon6[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A)$coefficients[,1],digits=3),digits=3)),star_function(summary(A)$coefficients[,4]))
  Result$Mon6[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A)$coefficients[,3],digits=3),digits=3),")")
  Result$Mon6[nrow(Result)-1] = Lag_zero(round(summary(A)$r.sq * 100, digits = 2),digits=2)
  Result$Mon6[nrow(Result)] = nrow(Mergers_New)
  
  A<-A3
  Result$Mon3[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A)$coefficients[,1],digits=3),digits=3)),star_function(summary(A)$coefficients[,4]))
  Result$Mon3[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A)$coefficients[,3],digits=3),digits=3),")")
  Result$Mon3[nrow(Result)-1] = Lag_zero(round(summary(A)$r.sq * 100, digits = 2),digits=2)
  Result$Mon3[nrow(Result)] = nrow(Mergers_New)
}

library(xtable)
print(xtable(Result),include.rownames=F,sanitize.text.function=function(x){x})


########################### Robustness Merger Regression #####################

if(specification=="purpose-based"){
  setDF(Panel_New_all)
  Temp = Panel_New_all[,c("Time","YearMonth","year","NAICS","PERMNO","Beta_M","Weight_Beta_M","Shrinking_Beta_M","Weight_Beta_CF","Weight_Beta_DR",
                          "Weight_Beta_Down","Weight_Beta_Up","Weighting_Shrinking_Beta_M","HHI","prodmktfluid","tnic3hhi","tnic3tsimm","Age","Momentum","Illiquidity","MktCap","Mkt_Book","Default_Spread",
                          "Financial_Leverage", "Operating_Leverage","Firm_size","InvestmentRates","ROE","Logat","Dividend_Dummy","q","I_Vol","Merger_Dummy","FluidMerger_Dummy","Merger_Dummy3","Merger_Dummy6","Merger_Dummy12","Merger_Dummy24",
                          "Merger_Dummy36","Merger_Dummy60")]#
  
  library(lfe)
  for(i in 1:dim(Temp)[2]){
    Temp[,i][Temp[,i] == Inf] = NA
    Temp[,i][Temp[,i] == -Inf] = NA
  }
  Temp = na.omit(Temp)
  
  source("Table_Functions.R")
  
  A = felm(Beta_M ~ Merger_Dummy24  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
             scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
  
  A_Weight = felm(Weight_Beta_M ~ Merger_Dummy24  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
                    scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
  
  A_Shrink = felm(Shrinking_Beta_M ~ Merger_Dummy24  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + scale(Dividend_Dummy) + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
                    scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
  
  A_ShrinkWeight = felm(Weighting_Shrinking_Beta_M ~ Merger_Dummy24  + scale(log(Age)) + scale(Logat) + scale(Default_Spread) + Dividend_Dummy + scale(Financial_Leverage) + scale(log(MktCap)) + scale(Illiquidity) + scale(InvestmentRates) + scale(I_Vol) +  
                          scale(log(Mkt_Book)) + scale(Momentum)  + scale(Operating_Leverage) +  scale(q)  + scale(ROE)  | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")
  
  
  Names = c("$M^D$","$log(Age)$","$log(AT)$","$Default~spread$","$Dividend$","$Financial~leverage$","$log(Firm~size)$","$Illiquidity$","$Investment~rate$","$iVol$",
            "$log(Mkt/Book)$","$Momentum$","$Operating~leverage$","$q$","$ROE$")
  
  Result = data.frame(Names=rep(NA,2*length(summary(A)$coef[,1])+2),Market=rep(NA,2*length(summary(A)$coef[,1])+2),'Weighting' = rep(NA,2*length(summary(A)$coef[,1])+2),
                      'Shrinking'=rep(NA,2*length(summary(A)$coef[,1])+2),'Weighting Shrinking' = rep(NA,2*length(summary(A)$coef[,1])+2))
  
  Result$Names[seq(from=1,to=(nrow(Result)-2),by=2)] = Names
  
  Result$Market[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A)$coefficients[,1],digits=3),digits=3)),star_function(summary(A)$coefficients[,4]))
  Result$Market[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A)$coefficients[,3],digits=3),digits=3),")")
  Result$Market[nrow(Result)-1] = Lag_zero(round(summary(A)$r.sq * 100, digits = 2),digits=2)
  Result$Market[nrow(Result)] = nrow(Mergers_New)
  
  Result$Weighting[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A_Weight)$coefficients[,1],digits=3),digits=3)),star_function(summary(A_Weight)$coefficients[,4]))
  Result$Weighting[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A_Weight)$coefficients[,3],digits=3),digits=3),")")
  Result$Weighting[nrow(Result)-1] = Lag_zero(round(summary(A_Weight)$r.sq * 100, digits = 2),digits=2)
  Result$Weighting[nrow(Result)] = nrow(Mergers_New)
  
  Result$Shrinking[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A_Shrink)$coefficients[,1],digits=3),digits=3)),star_function(summary(A_Shrink)$coefficients[,4]))
  Result$Shrinking[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A_Shrink)$coefficients[,3],digits=3),digits=3),")")
  Result$Shrinking[nrow(Result)-1] = Lag_zero(round(summary(A_Shrink)$r.sq * 100, digits = 2),digits=2)
  Result$Shrinking[nrow(Result)] = nrow(Mergers_New)
  
  Result$Weighting.Shrinking[seq(from=1,to=(nrow(Result)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(A_ShrinkWeight)$coefficients[,1],digits=3),digits=3)),star_function(summary(A_ShrinkWeight)$coefficients[,4]))
  Result$Weighting.Shrinking[seq(from=2,to=(nrow(Result)-2),by=2)] = paste0("(",Lag_zero(round(summary(A_ShrinkWeight)$coefficients[,3],digits=3),digits=3),")")
  Result$Weighting.Shrinking[nrow(Result)-1] = Lag_zero(round(summary(A_ShrinkWeight)$r.sq * 100, digits = 2),digits=2)
  Result$Weighting.Shrinking[nrow(Result)] = nrow(Mergers_New)
  
  
  
  library(xtable)
  print(xtable(Result),include.rownames=F,sanitize.text.function=function(x){x})
}